package longrise.phone.com.bjjt_jyb.Utils.icomet.message;

import com.google.gson.Gson;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class MessageInputStream
{
    private InputStream           input;

    //	private BufferedReader reader;
    private BufferedInputStream   reader;
    private int                   idx = 0;
    private ByteArrayOutputStream out = null;

    public MessageInputStream( InputStream input )
    {
        this.input = input;
        //		reader = new BufferedReader(new InputStreamReader(this.input));
        reader = new BufferedInputStream(input);
        out = new ByteArrayOutputStream(1024*4);
    }

    //    private static byte[] charToByte(char c) {
    //        byte[] b = new byte[2];
    //        b[0] = (byte) ((c & 0xFF00) >> 8);
    //        b[1] = (byte) (c & 0xFF);
    //        return b;
    //    }

    private String read () throws Exception
    {
//        StringBuilder b = new StringBuilder();
        while (true)
        {
            int cur = reader.read();
            if ( cur == '\n' )
            {
                //                return new String( b.toString().getBytes(),"UTF-8");
                try
                {
                    return new String(out.toByteArray(), "UTF-8");
                }
                finally
                {
                    out.reset();
                }
            }
            //            b.append((char)cur);
            out.write(cur);
        }
    }

    private String read2 () throws Exception
    {
        try
        {
            byte buf[] = new byte[1024];

            int n;
            while ((n = reader.read(buf)) != -1)
            {
                if ( n == 0 )
                {
                    int b = reader.read();
                    if ( b == -1 )
                    {
                        break;
                    }
                    else
                    {
                        n = 1;
                        buf[0] = (byte) b;
                    }
                }
                for (int i = 0; i < n; i++)
                {
                    if ( buf[i] == '\n' )
                    {
                        out.write(buf, 0, i);
                        byte[] bdata = out.toByteArray();
                        out.reset();
                        if ( i != n - 1 )
                            out.write(buf, i, n);
                        return new String(bdata, "UTF-8");
                    }
                }
                out.write(buf, 0, n);
            }
        }
        catch (Exception e)
        {

        }
        return null;
    }

    /**
     * get a message from the input stream, possibly a stream from sub connection
     *
     * @return a general message, caller should judge the type of this message and do the following work
     * @throws Exception
     */
    private Gson gson = new Gson();

    public Message readMessage () throws Exception
    {
        Message msg = null;

        String jsonData = this.read();
        if ( jsonData != null )
        {
//            System.out.println(jsonData);
            msg = gson.fromJson(jsonData, Message.class);
        }
        return msg;

    }
}
